// ProjectName: go-learn-demo
// PackageName: elasticybxb
// Description: 低级API操作索引
// Author: hy.bai
// Date: 2025-09-01 星期一 18:34

package elasticybxb

import (
	"fmt"
	"log/slog"
)

var LowIndex lowIndex

type lowIndex struct {
}

// 创建索引
func (*lowIndex) ElasticCreateIndex() {
	client, _ := LowInitElastic.InitElasticByUsername()
	// 低级API的方式创建索引
	resp, err := client.Indices.Create(LOW_INDEX_NAME)
	if err != nil {
		slog.Error(fmt.Sprintf("Elastic Create Index Error: %s", err.Error()))
		return
	}
	slog.Info(fmt.Sprintf("Elastic Create Index: %s", resp.String()))
}

// 查看索引
func (*lowIndex) ElasticGetIndex() {
	client, _ := LowInitElastic.InitElasticByUsername()
	// 低级API的方式查看索引
	resp, err := client.Indices.Get([]string{LOW_INDEX_NAME})
	if err != nil {
		slog.Error(fmt.Sprintf("Elastic Create Index Error: %s", err.Error()))
		return
	}
	// 打印结果
	fmt.Println(resp.String())
}

// 删除索引
func (*lowIndex) ElasticDeleteIndex() {
	client, _ := LowInitElastic.InitElasticByUsername()
	// 低级API的方式删除索引
	resp, err := client.Indices.Delete([]string{LOW_INDEX_NAME})
	if err != nil {
		slog.Error(fmt.Sprintf("Elastic Delete Index Error: %s", err.Error()))
		return
	}
	// 打印结果
	fmt.Println(resp.String())
}

func TestLowElasticIndex() {
	LowIndex.ElasticDeleteIndex()
}
